import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: TG
 * @Description: About this class
 * @Version : 1.0
 */
public class ZhszSignUtil {
    public static void main(String[] args) throws UnsupportedEncodingException {
        Map<String, String> req = new HashMap<>();
        req.put("amount", "200.000000000000");
        req.put("wallet_addr", "0xcdd555f59a3cbc04f9f95b75e6a3a2fd1a7661b6");
        req.put("time", "1621841328");
        req.put("transfer_id", "2882555");
        req.put("transfer_time", "2021-05-14 17:26:53");
        req.put("sign", "4843593244382883874b259be929f32d");
        String expectSign = doSign(req, "dd08a39288c89a0981356b030d395d7f");
        System.out.println(expectSign);
    }

    /**
     * 签名
     *
     * @param reqParamMap 请求参数
     * @param mySecret    我的密钥
     * @return
     */
    public static String doSign(Map<String, String> reqParamMap, String mySecret) throws UnsupportedEncodingException {
        if (Objects.isNull(reqParamMap) || mySecret == null) {
            return "";
        }
        //参数放入自然排序map容器
        Map<String, String> sortedMap = new TreeMap<>();
        for (String key : reqParamMap.keySet()) {
            //去除空键
            if (key == null || key.length() == 0 || key.equals("sign")) {
                continue;
            }
            //去除空值,  sign参数不参与签名
            if (reqParamMap.get(key) == null || reqParamMap.get(key).length() == 0) {
                continue;
            }
            sortedMap.put(key, reqParamMap.get(key));
        }

        //url参数拼接
        StringBuilder formUrlParam = new StringBuilder();
        boolean firstAppend = true;
        for (String key : sortedMap.keySet()) {
            if (!firstAppend) {
                formUrlParam.append("&");
            } else {
                firstAppend = false;
            }
            String value = URLEncoder.encode(sortedMap.get(key), "utf-8");
            formUrlParam.append(key + "=" + value);
        }
        //拼接密钥
        formUrlParam.append(mySecret);
        String formUrlParamStr = formUrlParam.toString();
        String md5Str = getMD5Str(formUrlParamStr);
        return md5Str;
    }

    /**
     * @param str 待加密字符串
     * @return
     */
    public static String getMD5Str(String str) {
        MessageDigest messageDigest = null;

        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        byte[] byteArray = messageDigest.digest();

        StringBuffer md5StrBuff = new StringBuffer();

        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                md5StrBuff.append("0").append(
                        Integer.toHexString(0xFF & byteArray[i]));
            else
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return md5StrBuff.toString();
    }

}
